﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace editor.app
{
    /// <summary>
    /// AreaWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AreaWindow : Window
    {
        comm.objst.DStar.Data.Area _area = null;
        Action<string, object[]> _cmdCallback = null;

        public AreaWindow(Action<string, object[]> cmdCallback, comm.objst.DStar.Data.Area area)
        {
            InitializeComponent();
            this.LoadConfig1();

            _cmdCallback = cmdCallback;
            _area = area;
            Fill();

            tbSize.KeyDown += new KeyEventHandler(tbSize_KeyDown);
            btnSetSize.Click += new RoutedEventHandler(btnSetSize_Click);

            tbMapSize.KeyDown += new KeyEventHandler(tbMapSize_KeyDown);
            tbMapBlockSize.KeyDown += new KeyEventHandler(tbMapBlockSize_KeyDown);
            btnMakePath.Click += new RoutedEventHandler(btnMakePath_Click);
        }

        void btnMakePath_Click(object sender, RoutedEventArgs e)
        {
            ReceiveCmd("makePath");
            Fill();
        }

        void btnSetSize_Click(object sender, RoutedEventArgs e)
        {
            uint w, h, mapSize, mapBlockSize;
            var ss = tbSize.Text.Split(',');
            if (ss.Length == 2 && uint.TryParse(ss[0], out w) &&
                uint.TryParse(ss[1], out h) &&
                uint.TryParse(tbMapSize.Text, out mapSize) &&
                uint.TryParse(tbMapBlockSize.Text, out mapBlockSize))
            {
                ReceiveCmd("setSize", (int)w, (int)h, (int)mapSize, (int)mapBlockSize);
            }
            Fill();
        }
        void tbSize_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnSetSize_Click(btnSetSize, new RoutedEventArgs());
            }
        }
        void tbMapBlockSize_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnSetSize_Click(btnSetSize, new RoutedEventArgs());
            }
        }
        void tbMapSize_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnSetSize_Click(btnSetSize, new RoutedEventArgs());
            }
        }

        void ReceiveCmd(string cmd, params object[] args)
        {
            if (_cmdCallback != null)
            {
                _cmdCallback(cmd, args);
            }
        }
        void Fill()
        {
            tbSize.Text = _area.Width + "," + _area.Height;
            tbMapSize.Text = _area.MapSize.ToString();
            tbMapBlockSize.Text = _area.MapBlockSize.ToString();
        }
    }
}
